
package com.tools.cleanmaster.controller;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;

import com.tools.cleanmaster.model.TrashType;
import com.tools.cleanmaster.model.item.VideoTrashItem;
import com.tools.cleanmaster.utils.FeatureConfig;
import com.tools.cleanmaster.utils.LogHelper;

import java.io.File;

public class VideoScanner extends TrashScanner {
    private static final String TAG = "VideoScanner";
    private Context mContext;

    public VideoScanner(Context context, TrashHandler handler) {
        super(context, handler);
        this.mContext = context;
    }

    @Override
    public void scan(final int increaseProgress) {
        if (!mIsScanning) {
            mIsScanning = true;
            doScan(increaseProgress);
            mIsScanning = false;
        }
    }

    private void doScan(int increaseProgress) {
        Cursor cursor = null;
        long startTime;
        if (FeatureConfig.DEBUG_LOG) {
            startTime = System.currentTimeMillis();
        }
        try {
            ContentResolver contentResolver = mContext.getContentResolver();
            cursor = contentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                    new String[] {
                            MediaStore.Video.Media.DATA, MediaStore.Video.Media.TITLE, MediaStore.Video.Media.DURATION,
                            MediaStore.MediaColumns.SIZE, MediaStore.Video.Media._ID
                    }, null, null, null);
            if (cursor == null) {
                mTrashHandler.onTrashTypeFinish(TrashType.VIDEO_FILE, 0);
                return;
            }
            int count = cursor.getCount();
            if (FeatureConfig.DEBUG_LOG) {
                LogHelper.i(TAG, "doScan, scanMediaImage cursor count=" + count);
            }
            if (count == 0) {
                mTrashHandler.updateProgress(increaseProgress, null);
                mTrashHandler.onTrashTypeFinish(TrashType.VIDEO_FILE, 0);
                return;
            }
            int index = 0;
            long totalSize = 0;
            while (cursor.moveToNext()) {
                if (mStopRequested) {
                    if (FeatureConfig.DEBUG_LOG) {
                        LogHelper.i(TAG,
                                "doScan, scanMediaImage mStopRequested, in while return");
                    }
                    return;
                }
                String path = cursor.getString(0);
                String title = cursor.getString(1);
                int duration = cursor.getInt(2);
                long size = cursor.getLong(3);
                long id = cursor.getLong(4);
                File file = new File(path);
                if (file.exists()) {
                    totalSize += size;
                    createTrashItemVideo(file, path, title, duration, size, id);
                }
                int progressFile = TrashScanner.calculateProgress(increaseProgress, index, count);
                mTrashHandler.updateProgress(progressFile, path);
                index++;
            }
            mTrashHandler.onTrashTypeFinish(TrashType.VIDEO_FILE, totalSize);
        } catch (Exception e) {
            if (FeatureConfig.DEBUG_LOG) {
                LogHelper.e(TAG, "scanMediaImage", e);
            }
            mTrashHandler.updateProgress(increaseProgress, null);
            mTrashHandler.onTrashTypeFinish(TrashType.VIDEO_FILE, 0);
            return;
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        if (FeatureConfig.DEBUG_LOG) {
            LogHelper.i(TAG,
                    "doScan, scanMediaImage end! time = "
                            + (System.currentTimeMillis() - startTime));
        }
    }

    private void createTrashItemVideo(File f, String path, String title, int duration,
                                           long size, long id) {
        VideoTrashItem trash = new VideoTrashItem();
        trash.trashType = TrashType.VIDEO_FILE;
        trash.filePath = path;
        trash.mFileName = f.getName();
        trash.filePath = path;
        trash.fileCount = 1;
        trash.size = size;
        trash.mDuration = calculatTime(duration);
        trash.mVideoName = title;
        trash.mId = id;
        trash.isSelectedDefault = trash.isSelected = false;
        mTrashesData.putTrash(TrashType.VIDEO_FILE, trash);
        if (FeatureConfig.DEBUG_LOG) {
            LogHelper.i(SUB_TAG, "[trash_video] filePath:" + trash.filePath + " size:"
                    + trash.size + " name:" + trash.mVideoName + " duration:" + duration + " id=" + id);
        }
    }

    // 将毫秒转化为 时：分：秒 格式
    private String calculatTime(int milliSecondTime) {

        int hour = milliSecondTime / (60 * 60 * 1000);
        int minute = (milliSecondTime - hour * 60 * 60 * 1000) / (60 * 1000);
        int seconds = (milliSecondTime - hour * 60 * 60 * 1000 - minute * 60 * 1000) / 1000;

        if (seconds >= 60) {
            seconds = seconds % 60;
            minute += seconds / 60;
        }
        if (minute >= 60) {
            minute = minute % 60;
            hour += minute / 60;
        }

        String sh;
        String sm;
        String ss;
        if (hour < 10) {
            sh = "0" + String.valueOf(hour);
        } else {
            sh = String.valueOf(hour);
        }
        if (minute < 10) {
            sm = "0" + String.valueOf(minute);
        } else {
            sm = String.valueOf(minute);
        }
        if (seconds < 10) {
            ss = "0" + String.valueOf(seconds);
        } else {
            ss = String.valueOf(seconds);
        }

        return sh + ":" + sm + ":" + ss;
    }

}
